package immutablecollections.misc;

import java.util.*;

public class ArrayIterator<T> implements Iterator<T>
{
    final private static Object[] emptyArray = new Object[0];
    final private T[] arrayItems;
    private int index;

    @SuppressWarnings("unchecked")
    public ArrayIterator()
    {
        this.arrayItems = (T[]) emptyArray;
        index = 0;
    }

    public ArrayIterator(T[] arrayItems)
    {
        this.arrayItems = arrayItems;
        index = 0;
    }

    public static <T> Iterator<T> on(T[] arrayItems)
    {
        return new ArrayIterator<T>(arrayItems);
    }

    @Override
    public boolean hasNext()
    {
        return index < arrayItems.length;
    }

    @Override
    public T next()
    {
        if (!hasNext())
        {
            throw new NoSuchElementException();
        }
        return arrayItems[index++];
    }

    @Override
    public void remove()
    {
        throw new UnsupportedOperationException();
    }

}
